"use strict";
cc._RF.push(module, '7111aJPaq9F87rWqOJ+hnYY', 'QQAds');
// Script/Ssg/API/QQ/QQAds.js

'use strict';

var mBannerAd = null;
var loop = true;
var interval = null;

var QQAds = cc.Class({

    statics: {
        /**
         * 播放banner
         */
        showBanner: function showBanner(isLoop) {
            if (isLoop) {
                this.loop = isLoop;
            }
            var self = this;
            var oldBannerAd = null;
            if (this.mBannerAd) {
                oldBannerAd = this.mBannerAd;
                this.mBannerAd = null;
            }
            this.mBannerAd = BK.Advertisement.createBannerAd({
                viewId: 1003,
                style: {
                    x: 0,
                    y: 0
                }
            });
            this.mBannerAd.onLoad(function () {
                Log.debug('QQbanner广告加载成功');
            });
            this.mBannerAd.onError(function (err) {
                //加载失败
                var msg = err.msg;
                var code = err.code;
                Log.error('广告加载失败 msg = ' + msg + ' code = ', code);
            });
            this.mBannerAd.show();
            if (interval) {
                clearTimeout(interval);
            }
            if (loop) {
                interval = setTimeout(function () {
                    self.showBanner(false);
                }, 6000);
            }
            if (oldBannerAd) {
                oldBannerAd.destory();
            }
        },

        /**
         * 隐藏广告
         */
        hideBanner: function hideBanner() {
            if (this.mBannerAd) {
                this.mBannerAd.hide();
                loop = false;
                if (this.interval) {
                    clearTimeout(this.interval);
                    this.interval = null;
                }
            }
        },

        showVideo: function showVideo(callback) {
            var videoAd = BK.Advertisement.createVideoAd();
            var isComplete = false;
            videoAd.onLoad(function () {
                videoAd.show();
                //加载成功
                BK.Script.log(1, 1, "onLoad");
            });

            videoAd.onPlayStart(function () {
                //开始播放
                // callback(true);
                BK.Script.log(1, 1, "onPlayStart");
            });

            videoAd.onPlayFinish(function () {
                BK.Script.log(1, 1, "onPlayFinish");
                // callback(true);
                isComplete = true;
                //console.log('视频看完了！！')
            });

            videoAd.onError(function (err) {
                //加载失败

                BK.Script.log(1, 1, "onError code:" + err.code + " msg:" + err.msg);
                callback(false);
            });

            videoAd.onClose(function () {
                SoundUtil.resumeMusic();
                callback(isComplete);
            });
        }

    }
});

module.exports = QQAds;

cc._RF.pop();